﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.Entities.Properties;

namespace Workflows.Components.Entities.Addresses
{
    [Serializable]
    public class AddressExtractorComponent:WorkflowComponentBase
    {
        #region props

        private AddressExtractorInstruction _Instruction;
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (AddressExtractorInstruction)value; }
        }
        private AddressExtractorOutput _Output;
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (AddressExtractorOutput)value; }
        }



        /// <summary></summary>
        public DataProviderType DbProvider
        {
            get
            {
                return this._Instruction.DbProvider;
            }
            set
            {
                this._Instruction.DbProvider = value;
            }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string SelectSql
        {
            get
            {
                return this._Instruction.SelectSql;
            }
            set
            {
                this._Instruction.SelectSql = value;
            }
        }

        /// <summary></summary>
        public string PKField
        {
            get
            {
                return this._Instruction.PKField;
            }
            set
            {
                this._Instruction.PKField = value;
            }
        }

        /// <summary></summary>
        public string FullAddressField
        {
            get
            {
                return this._Instruction.FullAddressField;
            }
            set
            {
                this._Instruction.FullAddressField = value;
            }
        }

        /// <summary></summary>
        public DataProviderType TgtDbProvider
        {
            get
            {
                return this._Instruction.TgtDbProvider;
            }
            set
            {
                this._Instruction.TgtDbProvider = value;
            }
        }

        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._Instruction.TgtConnStr;
            }
            set
            {
                this._Instruction.TgtConnStr = value;
            }
        }

        /// <summary></summary>
        public string TgtTable
        {
            get
            {
                return this._Instruction.TgtTable;
            }
            set
            {
                this._Instruction.TgtTable = value;
            }
        }

        /// <summary></summary>
        public string AddrIDField
        {
            get
            {
                return this._Instruction.AddrIDField;
            }
            set
            {
                this._Instruction.AddrIDField = value;
            }
        }

        /// <summary></summary>
        public string RefKeyField
        {
            get
            {
                return this._Instruction.RefKeyField;
            }
            set
            {
                this._Instruction.RefKeyField = value;
            }
        }

        /// <summary></summary>
        public string StreetField
        {
            get
            {
                return this._Instruction.StreetField;
            }
            set
            {
                this._Instruction.StreetField = value;
            }
        }

        /// <summary></summary>
        public string SubDivField
        {
            get
            {
                return this._Instruction.SubDivField;
            }
            set
            {
                this._Instruction.SubDivField = value;
            }
        }

        /// <summary></summary>
        public string OrgField
        {
            get
            {
                return this._Instruction.OrgField;
            }
            set
            {
                this._Instruction.OrgField = value;
            }
        }

        /// <summary></summary>
        public string CityField
        {
            get
            {
                return this._Instruction.CityField;
            }
            set
            {
                this._Instruction.CityField = value;
            }
        }

        /// <summary></summary>
        public string RegionField
        {
            get
            {
                return this._Instruction.RegionField;
            }
            set
            {
                this._Instruction.RegionField = value;
            }
        }

        /// <summary></summary>
        public string PostalCodeField
        {
            get
            {
                return this._Instruction.PostalCodeField;
            }
            set
            {
                this._Instruction.PostalCodeField = value;
            }
        }

        /// <summary></summary>
        public string CountryField
        {
            get
            {
                return this._Instruction.CountryField;
            }
            set
            {
                this._Instruction.CountryField = value;
            }
        }
        #endregion

        public AddressExtractorComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Entity_Relations.ToString();
            this.ComponentName = "Address";
            this.Description = "Parse address given full address list";
            this.ComponentIcon = Resources.netprof_3006;
            this.ComponentImage = Resources.house_031126;

            this._Instruction = new AddressExtractorInstruction(
                this.ComponentID, Guid.Empty,
                DataProviderType.MSSQL, string.Empty, string.Empty,
                string.Empty, string.Empty,
                DataProviderType.MSSQL, string.Empty, string.Empty,
                string.Empty, string.Empty,
                string.Empty, string.Empty, string.Empty,
                string.Empty, string.Empty, string.Empty, string.Empty);
            this._Output = new AddressExtractorOutput(
                this.ComponentID, Guid.Empty, 0, 0, 0, 0);
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                AddressExtractorComponent component = base.Instantiate(xNode) as AddressExtractorComponent;

                component.DbProvider = (DataProviderType)int.Parse(
                                                                XmlDataUtil.GetAttributeValue(xNode, "DbProvider",
                                                                                              ((int)
                                                                                               DataProviderType.MSSQL).
                                                                                                  ToString()));
                component.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                component.SelectSql = XmlDataUtil.GetAttributeValue(xNode, "SelectSql", "");
                component.PKField = XmlDataUtil.GetAttributeValue(xNode, "PKField", "");
                component.FullAddressField = XmlDataUtil.GetAttributeValue(xNode, "FullAddressField", "");

                component.TgtDbProvider =
                    (DataProviderType)
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "TgtDbProvider",
                                                            ((int)DataProviderType.MSSQL).ToString()));
                component.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                component.TgtTable = XmlDataUtil.GetAttributeValue(xNode, "TgtTable", "");
                component.AddrIDField = XmlDataUtil.GetAttributeValue(xNode, "AddrIDField", "");
                component.RefKeyField = XmlDataUtil.GetAttributeValue(xNode, "RefKeyField", "");

                component.StreetField = XmlDataUtil.GetAttributeValue(xNode, "StreetField", "");
                component.SubDivField = XmlDataUtil.GetAttributeValue(xNode, "SubDivisionField", "");
                component.OrgField = XmlDataUtil.GetAttributeValue(xNode, "OrgField", "");
                component.CityField = XmlDataUtil.GetAttributeValue(xNode, "CityField", "");
                component.RegionField = XmlDataUtil.GetAttributeValue(xNode, "RegionField", "");
                component.PostalCodeField = XmlDataUtil.GetAttributeValue(xNode, "PostalCodeField", "");
                component.CountryField = XmlDataUtil.GetAttributeValue(xNode, "CountryField", "");
                

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }

      

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DbProvider", ((int)this.DbProvider).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSql", this.SelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PKField", this.PKField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FullAddressField", this.FullAddressField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtDbProvider", ((int)this.TgtDbProvider).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtTable", this.TgtTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AddrIDField", this.AddrIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RefKeyField", this.RefKeyField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StreetField", this.StreetField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubDivisionField", this.SubDivField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OrgField", this.OrgField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CityField", this.CityField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RegionField", this.RegionField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PostalCodeField", this.PostalCodeField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CountryField", this.CountryField);

            return xNode;
        }
    }
}
